package com.cssw.fyzb.core.dao.live;

import com.cssw.fyzb.core.model.dodb.*;
import com.ydn.dbframe.plugin.activerecord.Page;
import com.ydn.dbframe.plugin.activerecord.parse.SqlParse;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class AnchorDaoImpl implements AnchorDao {

    /**
     * 通过主播id获取主播信息
     *
     * @return
     */
    @Override
    public CmfAnchor queryAnchorById(Long anchorId) {
        return CmfAnchor.dao.findById(anchorId);
    }

    /**
     * 通过用户id获取主播信息
     *
     * @return
     */
    @Override
    public CmfAnchor queryAnchorByUserId(Long userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        SqlParse sqlParse = new SqlParse(params);
        sqlParse.addSQL("select * from cmf_anchor where user_id =:userId");
        return CmfAnchor.dao.findFirst(sqlParse);
    }

    /**
     * 修改用户状态
     *
     * @param anchorId
     * @param userId
     * @param status
     */
    public void queryAnchorSaveSate(Long anchorId, Long userId, String status) {
        CmfAnchor cmfAnchor = new CmfAnchor();
        cmfAnchor.setAnchorId(anchorId);
        cmfAnchor.setUserId(userId);
        cmfAnchor.setStatus(status);
        cmfAnchor.update();
    }

    public Boolean queryAnchorSaveSate(Long anchorId, String status) {
        CmfAnchor cmfAnchor = new CmfAnchor();
        cmfAnchor.setAnchorId(anchorId);
        cmfAnchor.setStatus(status);
        return cmfAnchor.update();
    }

    public CmfAnchorBlacklist queryAnchorBlacklistById(Long id) {
        return CmfAnchorBlacklist.dao.findById(id);
    }

    public CmfAnchorBlacklist queryAnchorBlacklist(Long anchorId, Long userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("anchorId", anchorId);
        params.put("userId", userId);
        SqlParse sqlParse = new SqlParse(params);
        sqlParse.addSQL("select * from cmf_anchor_blacklist where user_id=:userId and anchor_id=:anchorId");
        CmfAnchorBlacklist cmfAnchorBlacklist = CmfAnchorBlacklist.dao.findFirst(sqlParse);
        return cmfAnchorBlacklist;
    }

    @Override
    public Long addAnchorBlacklist(Long anchorId, Long userId) {
        CmfAnchorBlacklist cmfAnchorBlacklist = new CmfAnchorBlacklist();
        cmfAnchorBlacklist.setAnchorId(anchorId);
        cmfAnchorBlacklist.setUserId(userId);
        cmfAnchorBlacklist.setCreateTime(new Date());
        cmfAnchorBlacklist.save();
        return cmfAnchorBlacklist.getBlacklistId();
    }

    @Override
    public void removeAnchorBlacklistById(Long id) {
        CmfAnchorBlacklist cmfAnchorBlacklist = CmfAnchorBlacklist.dao.findById(id);
        if (cmfAnchorBlacklist == null) {
            return;
        }
        cmfAnchorBlacklist.delete();
    }

    @Override
    public Page<CmfAnchorBlacklist> listAnchorBlacklist(Long anchorId, Integer pageNumber, Integer pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("anchorId", anchorId);
        SqlParse sqlParse = new SqlParse(params);
        sqlParse.addSQL("select t1.*,t2.user_name,t2.avatar ");
        sqlParse.addSQL("from cmf_anchor_blacklist t1  ");
        sqlParse.addSQL(" left join t_user t2 on t1.user_id=t2.user_id");
        sqlParse.addSQL(" where t1.anchor_id=:anchorId ");
        return CmfAnchorBlacklist.dao.paginate(pageNumber, pageSize, sqlParse);
    }
    @Override
    public List<CmfAnchorBlacklist> listAnchorBlacklist(Long anchorId) {
        Map<String, Object> params = new HashMap<>();
        params.put("anchorId", anchorId);
        SqlParse sqlParse = new SqlParse(params);
        sqlParse.addSQL("select t1.*,t2.user_name,t2.avatar ");
        sqlParse.addSQL("from cmf_anchor_blacklist t1  ");
        sqlParse.addSQL(" left join t_user t2 on t1.user_id=t2.user_id");
        sqlParse.addSQL(" where t1.anchor_id=:anchorId ");
        return CmfAnchorBlacklist.dao.find(sqlParse);
    }

    public CmfAnchorManage queryAnchorManageById(Long id) {
        return CmfAnchorManage.dao.findById(id);
    }

    public CmfAnchorManage queryAnchorManage(Long anchorId, Long userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("anchorId", anchorId);
        params.put("userId", userId);
        SqlParse sqlParse = new SqlParse(params);
        sqlParse.addSQL("select * from cmf_anchor_manage where user_id=:userId and anchor_id=:anchorId");
        CmfAnchorManage cmfAnchorManage = CmfAnchorManage.dao.findFirst(sqlParse);
        return cmfAnchorManage;
    }

    @Override
    public Long addAnchorManage(Long anchorId, Long userId) {
        CmfAnchorManage cmfAnchorManage = new CmfAnchorManage();
        cmfAnchorManage.setAnchorId(anchorId);
        cmfAnchorManage.setUserId(userId);
        cmfAnchorManage.save();
        return cmfAnchorManage.getManageId();
    }

    @Override
    public void removeAnchorManageById(Long id) {
        CmfAnchorManage cmfAnchorManage = CmfAnchorManage.dao.findById(id);
        if (cmfAnchorManage == null) {
            return;
        }
        cmfAnchorManage.delete();
    }

    @Override
    public Page<CmfAnchorManage> listAnchorManage(Long anchorId, Integer pageNumber, Integer pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("anchorId", anchorId);
        SqlParse sqlParse = new SqlParse(params);
        sqlParse.addSQL(" select t1.*,t2.user_name,t2.avatar from cmf_anchor_manage t1 left join t_user t2 on t1.user_id=t2.user_id");
        sqlParse.addSQL(" where t1.anchor_id=:anchorId");
        return CmfAnchorManage.dao.paginate(pageNumber, pageSize, sqlParse);
    }


    @Override
    public List<CmfAnchorManage> listAnchorManage(Long anchorId) {
        Map<String, Object> params = new HashMap<>();
        params.put("anchorId", anchorId);
        SqlParse sqlParse = new SqlParse(params);
        sqlParse.addSQL(" select t1.*,t2.user_name,t2.avatar from cmf_anchor_manage t1 left join t_user t2 on t1.user_id=t2.user_id ");
        sqlParse.addSQL(" where t1.anchor_id=:anchorId");
        return CmfAnchorManage.dao.find(sqlParse);
    }

    public CmfAnchorSensitive queryAnchorSensitiveById(Long id) {
        return CmfAnchorSensitive.dao.findById(id);
    }

    public CmfAnchorSensitive queryAnchorSensitive(Long anchorId, String keyWord) {
        Map<String, Object> params = new HashMap<>();
        params.put("anchorId", anchorId);
        params.put("keyWord", keyWord);
        SqlParse sqlParse = new SqlParse(params);
        sqlParse.addSQL("select * from cmf_anchor_sensitive where key_word=:keyWord and anchor_id=:anchorId");
        CmfAnchorSensitive cmfAnchorSensitive = CmfAnchorSensitive.dao.findFirst(sqlParse);
        return cmfAnchorSensitive;
    }

    @Override
    public Long addAnchorSensitive(Long anchorId, String keyWord) {
        CmfAnchorSensitive cmfAnchorSensitive = new CmfAnchorSensitive();
        cmfAnchorSensitive.setAnchorId(anchorId);
        cmfAnchorSensitive.setKeyWord(keyWord);
        cmfAnchorSensitive.save();
        return cmfAnchorSensitive.getSensitiveId();
    }

    @Override
    public void removeAnchorSensitiveById(Long id) {
        CmfAnchorSensitive cmfAnchorSensitive = CmfAnchorSensitive.dao.findById(id);
        if (cmfAnchorSensitive == null) {
            return;
        }
        cmfAnchorSensitive.delete();
    }

    @Override
    public Page<CmfAnchorSensitive> listAnchorSensitive(Long anchorId, Integer pageNumber, Integer pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("anchorId", anchorId);
        SqlParse sqlParse = new SqlParse(params);
        sqlParse.addSQL("select * from cmf_anchor_sensitive where anchor_id=:anchorId");
        return CmfAnchorSensitive.dao.paginate(pageNumber, pageSize, sqlParse);
    }

    @Override
    public void updateLiveByAnchor(Long anchorId, String live) {
        CmfAnchor anchor = queryAnchorById(anchorId);
        if (anchor != null) {
            anchor.setLive(live);
            anchor.update();
        }
    }

    @Override
    public void startLive(Long anchorId, String thumb, String title) {
        CmfAnchor anchor = new CmfAnchor();
        anchor.setAnchorId(anchorId);
        anchor.setThumb(thumb);
        anchor.setTitle(title);
        anchor.setLive(CmfAnchor.STATUS_LIVING);
        anchor.update();

    }

    @Override
    public Page<CmfAnchor> listAnchor(int page, int limit, String search) {
        Map<String, Object> params = new HashMap<>();
        params.put("search",search);
        SqlParse sqlParse = new SqlParse(params);
        sqlParse.addSQL("SELECT a.*,b.nick_name,b.mobile ");
        sqlParse.addSQL(" FROM cmf_anchor a LEFT JOIN t_user b ON a.user_id=b.user_id ");
        sqlParse.addSQL(" WHERE 1=1 ");
        sqlParse.addLikeSQL(" AND (nick_name LIKE :search ");
        sqlParse.addLikeSQL(" OR mobile LIKE :search) ");
        return CmfAnchor.dao.paginate(page, limit, sqlParse);
    }

    @Override
    public Long saveAnchor(Long userId) {
        CmfAnchor anchor = new CmfAnchor();
        anchor.setUserId(userId);
        anchor.setLive(CmfAnchor.STATUS_LIVE_END);
        anchor.setType(CmfAnchor.STATUS_TYPE);
        anchor.setCreateTime(new Date());
        anchor.setStatus(CmfAnchor.STATUS_STATUS_AVAILABLE);
        anchor.save();
        return anchor.getAnchorId();
    }


    @Override
    public void updateTitle(Long anchorId, String title) {
        CmfAnchor c = CmfAnchor.dao.findById(anchorId);
        c.setTitle(title);
        c.update();
    }

    @Override
    public void updateThumb(Long anchorId, String thumb) {
        CmfAnchor c = CmfAnchor.dao.findById(anchorId);
        c.setThumb(thumb);
        c.update();
    }

    @Override
    public CmfAnchor getUserId(Long userId) {
        StringBuilder str = new StringBuilder();
        str.append("select * from cmf_anchor where user_id=? ");
        return CmfAnchor.dao.findFirst(str.toString(), userId);
    }


}
